package com.office.quartz.service.impl;

import com.office.common.config.FromUserEmailConfig;
import com.office.common.utils.email.EmailAsyncWrapper;
import com.office.quartz.domain.PostAReminder;
import com.office.quartz.mapper.PostAReminderMapper;
import com.office.quartz.service.PostAReminderService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author: 柒月
 * @date: 2025/7/9 下午4:11
 * @version: 1.0
 */
@Service
public class PostAReminderServiceImpl implements PostAReminderService {

    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Resource
    public PostAReminderMapper postAReminderMapper;

    // 异步的发送邮件
    @Resource
    private EmailAsyncWrapper EmailAsyncWrapper;

    @Resource
    private FromUserEmailConfig fromUserEmailConfig;

    /**
     * @return 查询提醒是否有到时间了的
     */
    @Override
    public List<PostAReminder> checkIfTheReminderIsUpToDate() {
        logger.info("查询提醒是否有到时间了的提醒日程");
        return postAReminderMapper.checkIfTheReminderIsUpToDate();
    }

    /**
     * @param list 到时间的任务 需要发送给用户
     */
    @Override
    public void sendAReminder(List<PostAReminder> list) {
        for (PostAReminder postAReminder : list) {
            // 发送邮件
            EmailAsyncWrapper.sendReminderEmailAsync(
                    fromUserEmailConfig.getFromUserEmail(), postAReminder.getEmail(),
                    postAReminder.getTitle(), postAReminder.getMessage());

        }
        // 修改表状态
        postAReminderMapper.updateReminderInfo(list);
    }
}
